package com.jcy.festec;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.jcy.latte_core.delegates.LatteDelegate;
import com.jcy.latte_core.net.RestClient;
import com.jcy.latte_core.net.RestCreator;
import com.jcy.latte_core.net.callBack.IError;
import com.jcy.latte_core.net.callBack.IFailure;
import com.jcy.latte_core.net.callBack.ISuccess;
import com.jcy.latte_core.net.rx.RxRestClient;

import java.util.WeakHashMap;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AppDelegate extends LatteDelegate {
    @Override
    public Object setLayout() {
        return R.layout.delegate_app;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        testRestClient();
    }

    private void testRestClient() {
        RestClient.builder()
                .url("http://127.0.0.1/index")
                .loader(getContext())
                .success(response -> {
                    Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
                })
                .failure(() -> {

                })
                .error((code, msg) -> {

                })
                .build()
                .get();

        // 第一种Rx
        WeakHashMap<String,Object> weakHashMap = new WeakHashMap<>();
        weakHashMap.put("aaa","123");
        RestCreator.getRxRestService()
                .get("",weakHashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        // 第二种Rx
        RxRestClient.builder()
                .url("")
                .build()
                .get()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
